﻿using System;
using System.Runtime.Serialization;

namespace Birkelund.QueryStrings
{
    /// <summary>
    /// Exception thrown on token parser errors 
    /// </summary>
    [Serializable]
    public class TokenParserException : Exception
    {
        /// <summary>
        /// Gets or sets the FailureArgs.
        /// </summary>
        /// <value>The FailureArgs.</value>
        public FailureArgs FailureArgs { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TokenParserException"/> class.
        /// </summary>
        /// <param name="failureArgs">The FailureArgs.</param>
        public TokenParserException(FailureArgs failureArgs)
            : base(@"An exception occured while reading a query string token." + Environment.NewLine + "Details: " + failureArgs.ToString())
        {
            FailureArgs = failureArgs;
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="TokenParserException"/> class.
        /// </summary>
        /// <param name="info">The object that holds the serialized object data.</param>
        /// <param name="context">The contextual information about the source or destination.</param>
        protected TokenParserException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            FailureArgs = (FailureArgs)info.GetValue("FailureArgs", typeof(FailureArgs));
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// The <paramref name="info"/> parameter is a null reference (Nothing in Visual Basic).
        /// </exception>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
        /// </PermissionSet>
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FailureArgs", FailureArgs);
            base.GetObjectData(info, context);
        }
    }
}